package com.guochenglang.www.controller;

import com.guochenglang.www.pojo.Article;
import com.guochenglang.www.pojo.Files;
import com.guochenglang.www.pojo.Page;
import com.guochenglang.www.service.ArticleService;
import com.guochenglang.www.service.SearchService;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.util.GetObject;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.RequestReCodeUtil;
import com.guochenglang.www.view.vo.ArticleView;
import com.guochenglang.www.web.result.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 文章Controller层
 *
 * @author GuoChenglang
 */

public class ArticleController {
    private static final ArticleService ARTICLE_SERVICE = MyDependency.returnObject(ArticleService.class);
    private static final UserService USER_SERVICE = MyDependency.returnObject(UserService.class);
    private static final SearchService SEARCH_SERVICE = MyDependency.returnObject(SearchService.class);

    /**
     * 删除文章
     */
    public static Result deleteArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得id
        int id = GetObject.getObject(req, Integer.class);
        int num = ARTICLE_SERVICE.deleteArticle(id);
        return Result.result(num);
    }

    /**
     * 发表文章
     */
    public static Result uploadArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, InterruptedException, IllegalAccessException {
        //编码设置
        RequestReCodeUtil.reCode(req, resp);
        //获得文章
        Article article = GetObject.getObject(req, Article.class);
        //设置创建时间
        article.setCreatTime(new Timestamp(System.currentTimeMillis()));
        int num = ARTICLE_SERVICE.publishArticle(article);
        if (num < 0) {
            return Result.dangerWord();
        }
        return Result.result(num);
    }


    /**
     * 获得页面图片数据
     */
    public static Result getImage(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得文章id
        Integer articleId = Integer.parseInt(req.getParameter("articleId"));
        //根据id获得文件
        ArrayList<Files> files = ARTICLE_SERVICE.showFiles(articleId);
        ArrayList<String> urls = new ArrayList<>();
        //逐个获得文件的url
        for (Files file : files) {
            urls.add(file.getAddress());
        }
        return Result.result(urls);
    }

    /**
     * 根据点赞数展示文章
     */
    public static Result showArticleByLike(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer size = Integer.valueOf(req.getParameter("size"));
        Page<Article> articles = ARTICLE_SERVICE.allArticleByLike(page, size);
        return Result.result(articles);

    }

    /**
     * 根据日期展示文章
     */
    public static Result showArticleByTime(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer size = Integer.valueOf(req.getParameter("size"));
        Page<Article> articles = ARTICLE_SERVICE.allArticleByDate(page, size);
        return Result.result(articles);

    }

    /**
     * 搜索文章
     */
    public static Result searchArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String keyword = req.getParameter("input");
        keyword = new String(keyword.getBytes("ISO8859-1"), "utf-8");
        return Result.result(SEARCH_SERVICE.articleSearch(keyword));
    }

    /**
     * 获得知识库文章
     */
    public static Result baseArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //获得知识库id
        ArrayList<ArticleView> articleViews = new ArrayList<>();
        int baseId = Integer.parseInt(req.getParameter("baseId"));
        ArrayList<Article> articles = ARTICLE_SERVICE.baseArticle(baseId);
        //获得所有作者用户名，用户页面展示
        for (Article article : articles) {
            ArticleView articleView = new ArticleView();
            articleView.setArticle(article);
            String username = USER_SERVICE.getUser(article.getCreaterId()).getUsername();
            articleView.setAuthor(username);
            articleViews.add(articleView);
        }
        return Result.result(articleViews);
    }

    /**
     * 根据id获取文章
     */
    public static Result getArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException, InstantiationException, SQLException, InterruptedException, IllegalAccessException {
        //获取文章id
        Integer articleId = GetObject.getObject(req, Integer.class);

        System.out.println(articleId);
        //返回文章
        return Result.result(ARTICLE_SERVICE.getArticle(articleId));
    }
}
